package logic

import (
	"context"
	"errors"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoTopicShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoTopicShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoTopicShowLogic {
	return &VideoTopicShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoTopicShowLogic) VideoTopicShow(in *videorpc.VideoTopicShowRequest) (*videorpc.VideoTopicShowResponse, error) {
	// todo: add your logic here and delete this line

	var video mysql.VideoInfo

	err := video.FirstVideoById(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("该视频不存在")
	}

	var topic mysql.TopicInfo

	err = topic.FirstTopicById(mysql.DB, video.TopicId)
	if err != nil {
		return nil, errors.New("查询失败")
	}

	var user mysql.UserInfo

	err = user.FirstUserById(mysql.DB, video.UploadUserId)
	if err != nil {
		return nil, errors.New("查询失败")
	}

	return &videorpc.VideoTopicShowResponse{
		VideoTitle: video.VideoTitle,
		VideoIntro: video.VideoIntro,
		PlayNum:    int64(video.PlayNum),
		NickName:   user.NickName,
		AvatarLink: user.AvatarLink,
		TopicName:  topic.TopicName,
	}, nil
}
